﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPMS.Model;

namespace GPMS.Web.Controllers
{
    using GPMS.BLL;
    public class AnswerGroupController : BaseController
    {
        //
        // GET: /AnswerGroup/

        public ActionResult List()//答辩组列表
        {
            Guid id = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            return View(AnswerGroupHelper.GetList(gpmsDb, id));
        }

        public ActionResult UserList()//用户答辩组信息
        {
            Guid id = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            return View(AnswerGroupHelper.GetUserList(gpmsDb, id));
        }

        public ActionResult Create()//答辩组创建
        {
            ViewData["teachers"] = AccountHelper.GetTeachers(Request, gpmsDb);
            return View(new AnswerGroup() { Date = DateTime.Now });
        }

        [HttpPost]
        public ActionResult Create(AnswerGroup answergroup, FormCollection collection)
        {
            try
            {
                AnswerGroupHelper.Add(answergroup, gpmsDb);
                return Success(null);
            }
            catch (Exception ex)
            {
                return JsonError("新增失败:" + ex.Message);
            }
        }

        public ActionResult Edit(Guid id)//修改答辩组
        {
            ViewData["teachers"] = AccountHelper.GetTeachers(Request, gpmsDb);
            return View(gpmsDb.AnswerGroups.FirstOrDefault(a => a.AnswerGroupID == id));
        }

        [HttpPost]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            try
            {
                AnswerGroup answergroup = gpmsDb.AnswerGroups.FirstOrDefault(a => a.AnswerGroupID == id);
                UpdateModel(answergroup, collection);
                gpmsDb.SaveChanges();
                return Success("修改成功！！");
            }
            catch (Exception ex)
            {
                return JsonError("出错：" + ex.Message);
            }
        }

        public ActionResult Delete(Guid id)
        {
            return Confirm(string.Format("您确定删除答辩组：{0} 吗？", gpmsDb.AnswerGroups.FirstOrDefault(a => a.AnswerGroupID == id).Name));
        }
        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)
        {
            try
            {
                AnswerGroupHelper.Delete(id, gpmsDb);
                gpmsDb.SaveChanges();
                return Success("删除成功！！");
            }
            catch (Exception ex)
            {
                return JsonError("出错：" + ex.Message);
            }
        }

        public ActionResult Add(Guid id)//增加人员
        {
            return View(new AnswerGroupMember() { AnswerGroupID = id ,UserID=string.Empty});
        }
        [HttpPost]
        public ActionResult Add(AnswerGroupMember model, FormCollection collection)
        {
            AnswerGroup ag = gpmsDb.AnswerGroups.FirstOrDefault(a => a.AnswerGroupID == model.AnswerGroupID);
            UserInfo user = gpmsDb.UserInfos.FirstOrDefault(u => u.UserName == model.UserID);
            Setting setting = SettingHelper.Get(gpmsDb);
            if (gpmsDb.Roles.Any(r => r.RoleID == setting.TeacherRole && r.Users.Any(u => u.UserID == user.UserID)))
            {
                ag.Teachers.Add(user);
            }
            else if (gpmsDb.Roles.Any(r => r.RoleID == setting.StudentRole && r.Users.Any(u => u.UserID == user.UserID)))
            {
                ag.Students.Add(user);
            }
            else
            {
                return JsonError("该用户不是学生或教师");
            }
            gpmsDb.SaveChanges();
            return Success(null);
        }

        public ActionResult Remove(Guid id)//移除人员
        {
            return View("Add", new AnswerGroupMember() { AnswerGroupID = id, UserID = string.Empty });
        }

        [HttpPost]
        public ActionResult Remove(AnswerGroupMember model, FormCollection collection)
        {
            AnswerGroup ag = gpmsDb.AnswerGroups.FirstOrDefault(a => a.AnswerGroupID == model.AnswerGroupID);
            UserInfo user = gpmsDb.UserInfos.FirstOrDefault(u => u.UserName == model.UserID);
            Setting setting = SettingHelper.Get(gpmsDb);
            if(!gpmsDb.AnswerGroups.Any(a=>a.AnswerGroupID==model.AnswerGroupID && 
                (a.Teachers.Any(t=>t.UserID==user.UserID) || a.Students.Any(s=>s.UserID==user.UserID))))
            {
                return JsonError("该组成员没有此用户");
            }
            if (gpmsDb.Roles.Any(r => r.RoleID == setting.TeacherRole && r.Users.Any(u => u.UserID == user.UserID)))
            {
                ag.Teachers.Remove(user);
            }
            else if (gpmsDb.Roles.Any(r => r.RoleID == setting.StudentRole && r.Users.Any(u => u.UserID == user.UserID)))
            {
                ag.Students.Remove(user);
            }
            else
            {
                return JsonError("该用户不是学生或教师");
            }
            gpmsDb.SaveChanges();
            return Success(null);
        }

    }
}
